package com.xbongbong.saas.service.toolbox.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.pojo.dto.GetPackageStatusDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.PackageStatusEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 套餐校验帮助类
 * 创建时间 2019/7/4 4:37 PM
 * 修改时间 2019/7/4 4:37 PM
 * @author chy
 * @since v1.0
 * @version v1.0
 */
@Component
public class PackageStatusHelp {

    @Resource
    private UpdatePackageRedisService updatePackageRedisService;
    @Resource
    private UpdatePackageRedisHelp updatePackageRedisHelp;

    public Integer getPackageStatus(GetPackageStatusDTO getPackageStatusDTO) throws XbbException {

        String corpid = getPackageStatusDTO.getCorpid();
        ValueAddedTypeEnum valueAddedTypeEnum = getPackageStatusDTO.getValueAddedTypeEnum();
        XbbRefTypeEnum xbbRefTypeEnum = getPackageStatusDTO.getXbbRefTypeEnum();
        Long formId = getPackageStatusDTO.getFormId();

        JSONObject valueAddedJson4Redis = updatePackageRedisService.getValueAddInfo(corpid, valueAddedTypeEnum, xbbRefTypeEnum, formId);
        Integer totalValue = valueAddedJson4Redis.getInteger("totalValue");
        totalValue = totalValue == null ? 0 : totalValue;
        Integer usedValue = valueAddedJson4Redis.getInteger("usedValue");
        usedValue = usedValue == null ? 0 : usedValue;
        Integer count = getPackageStatusDTO.getCount();
        count = count == null ? 0 : count;
        Integer status = updatePackageRedisHelp.getPackageStatus(totalValue, (usedValue + count));
        return status;
    }

    /**
     * 获取套餐状态(非单表数据量){@link com.xbongbong.pro.enums.PackageStatusEnum}
     * @param corpid 公司id
     * @param valueAddedTypeEnum 增值业务
     * @param count 添加的数量
     * @return
     * @throws XbbException
     * 创建时间 2019/7/8 10:57 AM
     * 修改时间 2019/7/8 10:57 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    public Integer getPackageStatus(String corpid, ValueAddedTypeEnum valueAddedTypeEnum, Integer count) throws XbbException {
        return getPackageStatus(corpid, valueAddedTypeEnum, count, null, null);
    }

    /**
     * 获取套餐状态{@link com.xbongbong.pro.enums.PackageStatusEnum}
     * @param corpid 公司id
     * @param valueAddedTypeEnum 增值业务
     * @param count 添加的数量
     * @param xbbRefTypeEnum 指定saas业务的单表数据量时，需要传 参照 {@link com.xbongbong.pro.enums.SaasTableCountTypeEnum}
     * @param formId 自定义表单的单表数据量时，需要传
     * @return
     * @throws XbbException
     * 创建时间 2019/7/8 10:57 AM
     * 修改时间 2019/7/8 10:57 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    public Integer getPackageStatus(String corpid, ValueAddedTypeEnum valueAddedTypeEnum, Integer count, XbbRefTypeEnum xbbRefTypeEnum, Long formId) throws XbbException {
        GetPackageStatusDTO getPackageStatusDTO = new GetPackageStatusDTO();
        getPackageStatusDTO.setCorpid(corpid);
        getPackageStatusDTO.setValueAddedTypeEnum(valueAddedTypeEnum);
        getPackageStatusDTO.setCount(count);
        getPackageStatusDTO.setXbbRefTypeEnum(xbbRefTypeEnum);
        getPackageStatusDTO.setFormId(formId);
        return getPackageStatus(getPackageStatusDTO);
    }

    /**
     * 校验数据量
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param formId
     * @throws XbbException
     */
    public void validateData(String corpid , Integer saasMark, Integer businessType, Long formId) throws XbbException {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.PAAS;
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark)) {
            xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
        }
        Integer oneStatus = getPackageStatus(corpid, ValueAddedTypeEnum.ONE_FORM_DATA_NUM, 1, xbbRefTypeEnum,
                formId);
        Integer allStatus = getPackageStatus(corpid, ValueAddedTypeEnum.TOTAL_FORM_DATA_NUM, 1);
        if ( !Objects.equals(oneStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(oneStatus, PackageStatusEnum.WARNING.getCode()) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.SINGLE_TABLE_NUM_LIMIT);
        }

        if ( !Objects.equals(allStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(allStatus, PackageStatusEnum.WARNING.getCode()) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.TOTAL_TABLE_NUM_LIMIT);
        }
    }
}
